package com.liyunc.demo.controller;

import com.liyunc.demo.model.entity.Role;
import com.liyunc.demo.model.GeneralResponse;
import com.liyunc.demo.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping(value = "save")
    public GeneralResponse saveRole(@RequestParam String name) {
        Role role = new Role();
        role.setName(name);

        log.info("save role");
        roleService.saveRole(role);

        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("success");
        return response;
    }

    @DeleteMapping(value = "delete")
    public GeneralResponse deleteRole(@RequestParam Long id) {
        log.info("delete role");
        roleService.deleteRoleById(id);

        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("delete success");
        return response;
    }
}
